package com.social.config;

import com.social.config.realm.MyRealm;
import com.social.interceptor.MyLogoutFilter;
import com.social.service.UserService;
import com.social.serviceImpl.UserServiceImpl;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

/**
 * Shiro配置
 */
@Configuration
public class ShiroFilterConf {
    /**
     * 凭证管理
     *
     * @return 凭证管理器
     */
    @Bean
    public HashedCredentialsMatcher getHashedCredentialsMatcher() {
        //创建凭证匹配器
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //MD5加密算法
        hashedCredentialsMatcher.setHashAlgorithmName("Md5");
        //散列
        //hashedCredentialsMatcher.setHashIterations(1024);
        return hashedCredentialsMatcher;
    }

    /**
     * MyRealm
     *
     * @param hashedCredentialsMatcher 凭证管理器
     * @return 自定义realm对象
     */
    @Bean
    public MyRealm getMyRealm(HashedCredentialsMatcher hashedCredentialsMatcher) {
        //配置自定义realm
        MyRealm myRealm = myRealm();
        //将匹配器给自定义realm
        myRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return myRealm;
    }

    /**
     * @return 自定义realm对象
     */
    @Bean
    public MyRealm myRealm() {
        return new MyRealm();
    }

    /**
     * 缓存
     *
     * @return 缓存对象
     */
    @Bean
    public CacheManager getCacheManager() {
        CacheManager cacheManager = new EhCacheManager();
        return cacheManager;
    }

    /**
     * 安全管理器
     *
     * @param myRealm      自定义realm对象
     * @param cacheManager 缓存管理器
     * @return 默认web安全管理器
     */
    @Bean
    public SecurityManager getSecurityManager(MyRealm myRealm, CacheManager cacheManager) {
        //创建安全管理器
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        //将自定义realm交给安全管理器
        defaultWebSecurityManager.setRealm(myRealm);
        //配置缓存
        defaultWebSecurityManager.setCacheManager(cacheManager);
        return defaultWebSecurityManager;
    }

    /**
     * 过滤器工厂
     *
     * @param securityManager 安全管理器
     * @return 过滤器工厂对象
     */
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(SecurityManager securityManager) {
        //创建shiro过滤器工厂
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //将安全管理器交给shiro过滤器工厂
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //HashMap集合
        HashMap<String, String> map = new HashMap<String, String>();
        //拦截所有资源   authc过滤器简称 认证过滤器 认证成功才能访问  anon匿名过滤器 所有资源不需要认证可以访问
        map.put("/**", "authc");
        map.put("/User/login/**", "anon");
        map.put("/User/insert/**", "anon");
        map.put("/User/imgCode/**", "anon");
        map.put("/User/code/**", "anon");
        map.put("/css/**", "anon");
        map.put("/js/**", "anon");
        map.put("/plugins/**", "anon");
        map.put("/img/**", "anon");
        map.put("/login.jsp", "anon");
        map.put("/insert.jsp", "anon");
        map.put("/User/logout", "logout");
        //拦截策略 定义一个拦截器链
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        //自定义拦截跳转登陆页面
        shiroFilterFactoryBean.setLoginUrl("login.jsp");
        return shiroFilterFactoryBean;
    }
}
